/*
 * @lc app=leetcode.cn id=69 lang=cpp
 *
 * [69] Sqrt(x)
 */

// @lc code=start
class Solution
{
public:
    int mySqrt(int x)
    {
        //二分查找 寻找算数平方根
        int left = 1;
        int right = x;
        while (left <= right)
        {
            int mid = left + (right - left) / 2;
            if ((x / mid) > mid)
            {
                left = mid + 1;
            }
            else if ((x / mid) < mid)
            {
                right = mid - 1;
            }
            else
            {
                return mid;
            }
        }
        //因为一定在1-x的范围内 1是刚好返回mid 2是返回最接近x的平方根
        return right;
    }
};
// @lc code=end
